package org.watchrecords.pages.user;

import java.text.DateFormat;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.watchrecords.annotations.InstantiationPermission;
import org.watchrecords.constants.Permission;
import org.watchrecords.dbo.User;
import org.watchrecords.managers.UserManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.util.FormatHelper;

import com.google.inject.Inject;

@InstantiationPermission(Permission.VIEW_USERS)
public class UserListPage extends BasicPage {
    @Inject private UserManager userManager;
    
    public UserListPage(PageParameters pageParameters) {
        super(pageParameters);
        final DateFormat dateFormat = FormatHelper.getDateFormat();
        final boolean canEdit = haveAnyPermission(Permission.EDIT_USER);
        List<User> users = userManager.getUsers();
        
        WebMarkupContainer linkList = new WebMarkupContainer("linkList");
        Label actionColumn = new Label("actionColumn", "Action");
        
        add(linkList);
        add(actionColumn);
        
        if(!canEdit) {
            linkList.setVisible(false);
            actionColumn.setVisible(false);
        }
        
        add(new ListView<User>("userList", users) {
            private static final long serialVersionUID = -8897080956939132499L;

            @Override
            protected void populateItem(ListItem<User> item) {
                final User user = item.getModelObject();
                item.add(new Label("userName", user.getName()));
                item.add(new Label("registrationDate", dateFormat.format(user.getRegistrationDate())));
                item.add(new Label("role", ObjectUtils.toString(user.getRole(), "(none)")));
                
                WebMarkupContainer editColumn = new WebMarkupContainer("editColumn");
                item.add(editColumn);
                
                String characterName = StringUtils.defaultString(user.getCharacterName());
                String url = "http://argentarchives.org/node/" + user.getArgentNode();
                ExternalLink archivesLink = new ExternalLink("characterName", url, characterName);
                
                item.add(archivesLink);
                archivesLink.setVisible(!characterName.isEmpty());
                archivesLink.setEnabled(user.getArgentNode() != null);

                editColumn.add(new Link<Object>("editLink") {
                    private static final long serialVersionUID = -5877689330763109662L;

                    @Override
                    public void onClick() {
                        setResponsePage(EditUserPage.class, new PageParameters("userId=" + user.getId()));
                    }
                });
                
                editColumn.add(new Link<Object>("deleteLink") {
                    private static final long serialVersionUID = -5876689330763109662L;

                    @Override
                    public void onClick() {
                        userManager.deleteUser(user);
                        setResponsePage(UserListPage.class);
                    }
                });
                
                if(!canEdit) {
                    editColumn.setVisible(false);
                }
            }
        });
    }
}
